﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using KeRui.Common;
using KeRui.Model;
using System.Text;
using System.Collections.Generic;
using KeRui.TemplateMaker;
using System.IO;
using Admin = KeRui.BLL.Admin;
using CmsNode = KeRui.BLL.CmsNode;

public partial class Manage_Cms_ContentItem : System.Web.UI.Page
{
    private readonly string Act = ManageHelper.PageAct();
    private DataRow drCmsContentShow;
    private int modelID;
    protected string act;
    protected int nodeid = PageRequest.GetInt("nodeid", -1);//栏目
    protected int siteID = PageRequest.GetInt("siteid", -1);//站点
    protected int contentid = PageRequest.GetInt("contentid", -1);//文章
    protected KeRui.Model.CmsContent modelCmsContentShow = new CmsContent();
    protected string strCmsContentDetails, FieldForm;
    protected bool IsShowGetKeywords, IsShowEditor;

    protected int OP = PageRequest.GetInt("OP", -1);

    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            //检查登录HYC
            ManageHelper.CheckAdminLogin();

            //如果是编辑页面信息，则进行页面数据绑定
            if (contentid == -1)
            {
                KeRui.BLL.CmsNode bllCmsNode = new KeRui.BLL.CmsNode();
                modelID = bllCmsNode.GetModel(PageRequest.GetInt("nodeid", -1)).ModelID;
                FieldForm = GenerateForm(modelID);
                act = "add";
                if (OP == 1)
                {
                    modelCmsContentShow.Title = PageRequest.GetString("title");
                }
            }
            else
            {
                #region 编辑内容绑定数据
                act = "modify";
                KeRui.BLL.CmsNode bllCmsNode = new KeRui.BLL.CmsNode();
                KeRui.BLL.CmsContent bllCmsContent = new KeRui.BLL.CmsContent();
                modelCmsContentShow = bllCmsContent.GetModel(PageRequest.GetInt("contentid", -1));
                //获取站点ID
                siteID = bllCmsNode.GetModel(modelCmsContentShow.NodeID).SiteID;
                //基础内容显示
                //动态模型内容显示
                drCmsContentShow = bllCmsContent.GetModelByContentID(modelCmsContentShow.ContentID, "").Tables[0].Rows[0];
                //拼接页面表单显示的值
                KeRui.BLL.CmsField bllCmsField = new KeRui.BLL.CmsField();
                List<KeRui.Model.CmsField> list = bllCmsField.GetList("ModelID=" + modelCmsContentShow.ModelID + " AND IsUsing='True' AND FieldName NOT IN('Title','ContentID')");
                StringBuilder sb = new StringBuilder();
                foreach (CmsField model in list)
                {
                    switch (model.FieldType)
                    {
                        case 1:  //单行
                            switch (model.FieldName.ToLower())
                            {
                                case "editor":
                                    //KeRui.BLL.Admin bllAdmin = new Admin();
                                    //int UserID = KeRui.BLL.Account.GetLoginUserID();
                                    string RealName = "";
                                    KeRui.BLL.CmsContentForModelid BLLCmsContentForModelid = new KeRui.BLL.CmsContentForModelid();
                                    RealName = BLLCmsContentForModelid.GetModel("CmsContent0", contentid).Rows[0]["Editor"].ToString();
                                    //if (UserID > 0)
                                    //{
                                    //    RealName = bllAdmin.GetModel(UserID).RealName;
                                    //}
                                    sb.AppendFormat(
                                        "<tr id='{0}'><td align='left' width='80px'><img name='help1' src='../pic/icons/help.gif' alt='{1}' onclick='helpInf(this)' />&nbsp;<span>{1}：</span></td><td colspan='3'><input name='txt{0}' type='text'  value='{2}' class='colorblur' onfocus=\"this.className='colorfocus';\" onblur=\"this.className='colorblur';\" style='width:400px;' /></td></tr>", model.FieldName, model.FieldAlias, RealName);
                                    break;
                                case "keyword":
                                    IsShowGetKeywords = true;
                                    sb.AppendFormat(
                                        "<tr id='{0}'><td align='left' width='80px'><img name='help1' src='../pic/icons/help.gif' alt='{1}' onclick='helpInf(this)' />&nbsp;<span>{1}：</span></td><td colspan='3'><input name='txt{0}' type='text' id='txtkeywords' class='colorblur' value='{2}' onfocus=\"this.className='colorfocus';GetKeywords();\" onblur=\"this.className='colorblur';\" style='width:400px;' />&nbsp;<a href='javascript:void(0)' onclick='GetKeywords();return false;' >重新获取关键字</a>（以空格隔开）</td></tr>", model.FieldName, model.FieldAlias, drCmsContentShow[model.FieldName]);
                                    break;
                                default:
                                    sb.AppendFormat(
                                        "<tr id='{0}'><td align='left' width='80px'><img name='help1' src='../pic/icons/help.gif' alt='{1}' onclick='helpInf(this)' />&nbsp;<span>{1}：</span></td><td colspan='3'><input name='txt{0}' type='text' id='SubTitle' value='{2}' class='colorblur' onfocus=\"this.className='colorfocus';\" onblur=\"this.className='colorblur';\" style='width:400px;' /></td></tr>", model.FieldName, model.FieldAlias, drCmsContentShow[model.FieldName].ToString());
                                    break;
                            }
                            break;
                        case 8:  //数字型
                            sb.AppendFormat(
                                "<tr id='{0}'><td align='left' width='80px'><img name='help1' src='../pic/icons/help.gif' alt='{1}' onclick='helpInf(this)' />&nbsp;<span>{1}：</span></td><td colspan='3'><input name='txt{0}' type='text' id='SubTitle' value='{2}' class='colorblur' onfocus=\"this.className='colorfocus';\" onblur=\"this.className='colorblur';\" style='width:400px;' /></td></tr>", model.FieldName, model.FieldAlias, drCmsContentShow[model.FieldName]);
                            break;
                        case 2: //多行
                        case 3: //多行，HTML
                            if (model.FieldName == "Content") //富文本编辑器
                            {
                                IsShowEditor = true;
                                sb.AppendFormat(
                                "<tr id='{0}'><td colspan='4'><textarea id='txtContent' name='txtContent'>{1}</textarea></td></tr>", model.FieldName, drCmsContentShow["Content"]);
                                sb.Append("<tr ><td colspan='4'> <input type=\"button\" class=\"button\" value=\"自动排版\" onclick=\"Automatic_layout();\" /><input type=\"button\" class=\"button\" value=\"清除空行\" onclick=\"Remove_blank_lines();\" /><input type=\"button\" class=\"button\" value=\"清除JS\" onclick=\"Clear_JS()\" /><input type=\"button\" class=\"button\" value=\"添加空行\" onclick=\"Add_a_blank_line()\" /><input type=\"button\" class=\"button\" value=\"统计数字\" onclick=\"Statistics_words()\" /><input type=\"button\" class=\"button\" value=\"清空内容\" onclick=\"Empty()\" /></td></tr>");
                            }
                            else
                            {
                                sb.AppendFormat(
                                "<tr id='{0}'><td align='left' width='80px'><img name='help1' src='../pic/icons/help.gif' alt='{1}' onclick='helpInf(this)' />&nbsp;<span>{1}：</span></td><td colspan='3'><textarea id='txt{0}' name='txt{0}' style='width:400px; height:120px;' class='colorblur' onfocus=\"this.className='colorfocus';\" onblur=\"this.className='colorblur';\">{2}</textarea></td></tr>", model.FieldName, model.FieldAlias, drCmsContentShow[model.FieldName]);
                            }
                            break;
                        case 7: //下拉列表
                            sb.AppendFormat(
                               "<tr id='{0}'><td align='left' width='80px'><img name='help1' src='../pic/icons/help.gif' alt='{1}' onclick='helpInf(this)' />&nbsp;<span>{1}：</span></td><td colspan='3'><select name='txt{0}'>{2}</select></td></tr>", model.FieldName, model.FieldAlias, BuildSelectOption(model.StyleValue));
                            break;
                        case 9: //日期型
                            sb.Append("<script type='text/javascript'>$(document).ready(function(){$('#txt" + model.FieldName + "').bind('focus', function(){WdatePicker({isShowClear:false,readOnly:false})});});</script>");
                            sb.AppendFormat(
                                "<tr id='{0}'><td align='left' width='80px'><img name='help1' src='../pic/icons/help.gif' alt='{1}' onclick='helpInf(this)' />&nbsp;<span>{1}：</span></td><td colspan='3'><input name='txt{1}' id='txt{1}'  value='{2}' type='text' class='Wdate'/></td></tr>", model.FieldName, model.FieldAlias, drCmsContentShow[model.FieldName]);
                            break;
                        case 11: //字段附加属性
                            sb.AppendFormat(
                               "<tr id='{0}'><td align='left' width='80px'><img name='help1' src='../pic/icons/help.gif' alt='{1}' onclick='helpInf(this)' />&nbsp;<span>{1}：</span></td><td colspan='3'><select name='txt{0}'>{2}</select></td></tr>", model.FieldName, model.FieldAlias, BuildFieldAttOption(model.StyleValue, drCmsContentShow[model.FieldName].ToString()));
                            break;
                    }
                }
                strCmsContentDetails = sb.ToString();
                #endregion
            }
            switch (Act)
            {
                #region 添加文章

                case "add":
                    //检查是否有添加权限HYC
                    ManageHelper.CheckAdminPower("site_contentadd_" + siteID);
                    //添加Content，需要动态构建SQL语句
                    KeRui.Model.CmsContent modelCmsContent = new CmsContent();
                    KeRui.BLL.CmsContent bllCmsContent = new KeRui.BLL.CmsContent();
                    //添加Content的通用字段
                    modelCmsContent.NodeID = PageRequest.GetInt("parentnodeid", -1);

                    //ManageHelper.CheckAdminPower(siteID + "_" + modelCmsContent.NodeID + "node");

                    modelCmsContent.Title = PageRequest.GetString("txtTitle");
                    modelCmsContent.TitleStyle = ""; //标题的样式
                    modelCmsContent.IsRecommend = PageRequest.GetString("IsRecommend") == "1" ? true : false;
                    modelCmsContent.IsHot = PageRequest.GetString("IsHot") == "1" ? true : false;
                    modelCmsContent.IsTop = PageRequest.GetString("IsTop") == "1" ? true : false;
                    modelCmsContent.IsMarked = PageRequest.GetString("IsColor") == "1" ? true : false;
                    modelCmsContent.ContentState = PageRequest.GetInt("ContentState", -1);
                    modelCmsContent.UserID = KeRui.BLL.Account.GetLoginUserID();
                    modelCmsContent.EnableComment = PageRequest.GetInt("EnableComment", -1) == 1 ? true : false;
                    modelCmsContent.TemplateFile = PageRequest.GetString("templatelist");
                    modelCmsContent.PublishTime = DateTime.Parse(PageRequest.GetString("txtCreateTime"));
                    modelCmsContent.Summary = PageRequest.GetString("txtSummary");
                    modelCmsContent.PicUrl = PageRequest.GetString("txtPicUrl");
                    modelCmsContent.ContentType = PageRequest.GetInt("chkcontenttype", 0);
                    //审核成功
                    if (PageRequest.GetInt("ContentState", -1) == 2)
                    {
                        modelCmsContent.PublicState = 1;
                    }
                    //存储路径处理，前台可能需要自定义路径
                    modelCmsContent.FileDir = "";
                    modelCmsContent.FilePath = "";
                    //处理发布计划
                    switch (PageRequest.GetInt("publishType", -1))
                    {
                        case 0:
                            modelCmsContent.PublicStartTime = DateTime.Parse("1900-1-1");
                            modelCmsContent.PublicEndTime = DateTime.Parse("1900-1-1");
                            break;
                        case 1:
                            modelCmsContent.PublicStartTime = DateTime.Parse(PageRequest.GetString("StarteTime"));
                            modelCmsContent.PublicEndTime = DateTime.Parse(PageRequest.GetString("StarteTime"));
                            break;
                        case 2:
                            modelCmsContent.PublicStartTime = DateTime.Parse(PageRequest.GetString("StarteTime"));
                            modelCmsContent.PublicEndTime = DateTime.Parse(PageRequest.GetString("EndTime"));
                            break;
                        default:
                            modelCmsContent.PublicStartTime = DateTime.Parse("1900-1-1");
                            modelCmsContent.PublicEndTime = DateTime.Parse("1900-1-1");
                            break;
                    }
                    //动态的数据写入，插入到对应的Content表
                    StringBuilder sb = new StringBuilder();
                    sb.AppendFormat("INSERT INTO CmsContent{0} (", modelID);
                    //循环绑定插入字段
                    sb.Append("[ContentID],[Title],");
                    KeRui.BLL.CmsField bllCmsField = new KeRui.BLL.CmsField();
                    List<KeRui.Model.CmsField> list = bllCmsField.GetList("ModelID=" + modelID + " AND IsUsing='True' AND FieldName NOT IN('Title','ContentID')");
                    //字段名
                    StringBuilder sbFieldValue = new StringBuilder();
                    int contentID = bllCmsContent.Add(modelCmsContent);
                    sbFieldValue.AppendFormat("{0},", contentID); //添加基础Content内容信息
                    sbFieldValue.AppendFormat("'{0}',", KeRui.Common.Utils.SqlStringFormat(PageRequest.GetString("txtTitle").Replace("'", "\"")));
                    for (int i = 0; i < list.Count; i++)
                    {
                        if (i != list.Count - 1)
                        {
                            sb.AppendFormat("[{0}],", list[i].FieldName);
                            switch (list[i].FieldType)
                            {
                                case 8: //数字型，且不需要格式化
                                    sbFieldValue.Append(PageRequest.GetInt("txt" + list[i].FieldName, -1) + ",");
                                    break;
                                default:
                                    if (("txt" + list[i].FieldName) == "txtContent")
                                    {
                                        sbFieldValue.Append("'" + KeRui.Common.Utils.SqlStringFormat(PageRequest.GetString("txt" + list[i].FieldName).Replace("'", "\"").Replace("<p>\r\n	&nbsp;</p>", "")) + "',");
                                    }
                                    else
                                    {
                                        sbFieldValue.Append("'" + KeRui.Common.Utils.SqlStringFormat(PageRequest.GetString("txt" + list[i].FieldName)) + "',");
                                    }
                                    break;
                            }
                        }
                        else
                        {
                            sb.AppendFormat("[{0}]", list[i].FieldName);
                            switch (list[i].FieldType)
                            {
                                case 8:
                                    sbFieldValue.Append(PageRequest.GetInt("txt" + list[i].FieldName, -1)); //Bug:fixed 2009-11-30
                                    break;
                                default:
                                    if (("txt" + list[i].FieldName) == "txtContent")
                                    {
                                        if (!string.IsNullOrEmpty(PageRequest.GetString("txtLinkUrl")))
                                        {
                                            sbFieldValue.Append("'<script type=\"text/javascript\">onload=function(){window.location.href=\"" + PageRequest.GetString("txtLinkUrl") + "\";};</script>'");
                                        }
                                        else
                                        {
                                            sbFieldValue.Append("'" + KeRui.Common.Utils.SqlStringFormat(PageRequest.GetString("txt" + list[i].FieldName).Replace("'", "\"")) + "'");
                                        }
                                    }
                                    else
                                    {
                                        sbFieldValue.Append("'" + KeRui.Common.Utils.SqlStringFormat(PageRequest.GetString("txt" + list[i].FieldName)) + "'");
                                    }
                                    break;
                            }
                        }
                    }
                    sb.Append(") VALUES (");
                    //字段值
                    sb.Append(sbFieldValue);
                    sb.Append(")");
                    //string sql = KeRui.Common.Utils.SqlStringFormat(sb.ToString());
                    KeRui.BLL.Common.ExecuteSQL(sb.ToString()); //执行SQL语句
                    //如果是立即生成则生成
                    if (PageRequest.GetInt("publishType", -1) == 9 && PageRequest.GetInt("ContentState", -1) == 2)
                    {
                        Publish publish = new Publish();
                        try
                        {
                            publish.PublishContent(contentID);
                            int preID = bllCmsContent.GetModel(contentID).PrevID;
                            int nextID = bllCmsContent.GetModel(contentID).NextID;
                            if (preID != 0)
                            {
                                publish.PublishContent(preID);
                            }
                            if (nextID != 0)
                            {
                                publish.PublishContent(nextID);
                            }
                        }
                        catch
                        {
                            Response.Write("生成错误，请重新发布。");
                            Response.End();
                        }
                    }
                    //日志记录HYC
                    ManageHelper.AddLog("site_contentadd_" + siteID, "添加了文章：“" + modelCmsContent.Title + "”。");
                    //Response.Write("<script type=\"text/javascript\">window.close();</script>");
                    Response.Redirect("ContentList.aspx?siteid=" + PageRequest.GetString("siteid") + "&nodeid=" + PageRequest.GetString("nodeid"));
                    Response.End();
                    break;
                #endregion

                #region 修改文章
                case "modify":
                    //检查是否有修改权限HYC
                    ManageHelper.CheckAdminPower("site_contentupdate_" + siteID);
                    //添加Content的通用字段
                    KeRui.Model.CmsContent modelCmsContentModify = new CmsContent();
                    KeRui.BLL.CmsContent bllCmsContentModify = new KeRui.BLL.CmsContent();
                    modelCmsContentModify.ContentID = PageRequest.GetInt("contentid", -1);
                    modelCmsContentModify.NodeID = PageRequest.GetInt("parentnodeid", -1);

                    //ManageHelper.CheckAdminPower(siteID + "_" + modelCmsContentModify.NodeID + "node");


                    modelCmsContentModify.Title = PageRequest.GetString("txtTitle");
                    modelCmsContentModify.TitleStyle = ""; //标题的样式
                    modelCmsContentModify.IsRecommend = PageRequest.GetString("IsRecommend") == "1" ? true : false;
                    modelCmsContentModify.IsHot = PageRequest.GetString("IsHot") == "1" ? true : false;
                    modelCmsContentModify.IsTop = PageRequest.GetString("IsTop") == "1" ? true : false;
                    modelCmsContentModify.IsMarked = PageRequest.GetString("IsColor") == "1" ? true : false;
                    modelCmsContentModify.ContentState = PageRequest.GetInt("ContentState", -1);
                    modelCmsContentModify.UserID = KeRui.BLL.Account.GetLoginUserID();
                    modelCmsContentModify.ContentState = PageRequest.GetInt("ContentState", -1);
                    modelCmsContentModify.EnableComment = PageRequest.GetInt("EnableComment", -1) == 1 ? true : false;
                    modelCmsContentModify.TemplateFile = PageRequest.GetString("templatelist");
                    modelCmsContentModify.PublishTime = DateTime.Parse(PageRequest.GetString("txtCreateTime"));
                    modelCmsContentModify.Summary = PageRequest.GetString("txtSummary");
                    modelCmsContentModify.PicUrl = PageRequest.GetString("txtPicUrl");
                    modelCmsContentModify.ContentType = PageRequest.GetInt("chkcontenttype", 0);

                    //审核成功
                    if (PageRequest.GetInt("ContentState", -1) == 2)
                    {
                        modelCmsContentModify.PublicState = 1;
                    }
                    //存储路径处理，前台可能需要自定义路径
                    modelCmsContentModify.FileDir = "";
                    modelCmsContentModify.FilePath = "";
                    //处理发布计划
                    switch (PageRequest.GetInt("publishType", -1))
                    {
                        case 0:
                            modelCmsContentModify.PublicStartTime = DateTime.Parse("1900-1-1");
                            modelCmsContentModify.PublicEndTime = DateTime.Parse("1900-1-1");
                            break;
                        case 1:
                            modelCmsContentModify.PublicStartTime = DateTime.Parse(PageRequest.GetString("StarteTime"));
                            modelCmsContentModify.PublicEndTime = DateTime.Parse(PageRequest.GetString("StarteTime"));
                            break;
                        case 2:
                            modelCmsContentModify.PublicStartTime = DateTime.Parse(PageRequest.GetString("StarteTime"));
                            modelCmsContentModify.PublicEndTime = DateTime.Parse(PageRequest.GetString("EndTime"));
                            break;
                        default:
                            modelCmsContentModify.PublicStartTime = DateTime.Parse("1900-1-1");
                            modelCmsContentModify.PublicEndTime = DateTime.Parse("1900-1-1");
                            break;
                    }
                    bllCmsContentModify.Update(modelCmsContentModify);
                    //添加Content的动态字段
                    //构建Update语句
                    StringBuilder sbUpdate = new StringBuilder();
                    sbUpdate.AppendFormat("UPDATE CmsContent{0} SET	", bllCmsContentModify.GetModel(PageRequest.GetInt("contentid", -1)).ModelID);
                    KeRui.BLL.CmsField bllCmsFieldModify = new KeRui.BLL.CmsField();
                    List<KeRui.Model.CmsField> listModify = bllCmsFieldModify.GetList("ModelID=" + bllCmsContentModify.GetModel(PageRequest.GetInt("contentid", -1)).ModelID + " AND IsUsing='True' AND FieldName NOT IN('ContentID')");
                    //字段名
                    for (int i = 0; i < listModify.Count; i++)
                    {
                        if (i != listModify.Count - 1)
                        {
                            sbUpdate.AppendFormat("[{0}]=", listModify[i].FieldName);
                            switch (listModify[i].FieldType)
                            {
                                case 8: //数字型，且不需要格式化
                                    sbUpdate.Append(PageRequest.GetInt("txt" + listModify[i].FieldName, -1) + ",");
                                    break;
                                default:
                                    if (("txt" + listModify[i].FieldName) == "txtContent")
                                    {
                                        if (!string.IsNullOrEmpty(PageRequest.GetString("txtLinkUrl")))
                                        {
                                            sbUpdate.Append("'<script type=\"text/javascript\">onload=function(){window.location.href=\"" + PageRequest.GetString("txtLinkUrl") + "\";};</script>" + "',");
                                        }
                                        else
                                        {
                                            sbUpdate.Append("'" + KeRui.Common.Utils.SqlStringFormat(PageRequest.GetString("txt" + listModify[i].FieldName).Replace("'", "\"").Replace("<p>\r\n	&nbsp;</p>\r\n<p>\r\n	&nbsp;</p>", "")) + "',");
                                        }
                                    }
                                    else if (("txt" + listModify[i].FieldName) == "txtEditor") //修该编辑人
                                    {
                                        KeRui.BLL.Admin bllAdmin = new KeRui.BLL.Admin();
                                        sbUpdate.Append("'" + bllAdmin.GetModel(KeRui.BLL.Account.GetLoginUserID()).RealName + "',");
                                    }
                                    else
                                    {
                                        sbUpdate.Append("'" + KeRui.Common.Utils.SqlStringFormat(PageRequest.GetString("txt" + listModify[i].FieldName)) + "',");
                                    }
                                    break;
                            }
                        }
                        else
                        {
                            sbUpdate.AppendFormat("[{0}]=", listModify[i].FieldName);
                            switch (listModify[i].FieldType)
                            {
                                case 8:
                                    sbUpdate.Append(PageRequest.GetInt("txt" + listModify[i].FieldName, -1));
                                    break;
                                default:
                                    if (("txt" + listModify[i].FieldName) == "txtContent")
                                    {
                                        sbUpdate.Append("'" + KeRui.Common.Utils.SqlStringFormat(PageRequest.GetString("txt" + listModify[i].FieldName).Replace("'", "\"")) + "'");
                                    }
                                    else
                                    {
                                        sbUpdate.Append("'" + KeRui.Common.Utils.SqlStringFormat(PageRequest.GetString("txt" + listModify[i].FieldName)) + "'");
                                    }
                                    break;
                            }
                        }
                    }
                    //Update语句的where条件
                    sbUpdate.AppendFormat(" WHERE ContentID={0}", PageRequest.GetInt("contentid", -1));
                    KeRui.BLL.Common.ExecuteSQL(sbUpdate.ToString()); //执行SQL语句
                    //如果是立即生成则生成
                    if (PageRequest.GetInt("publishType", -1) == 9 && PageRequest.GetInt("ContentState", -1) == 2)
                    {
                        Publish publish = new Publish();
                        publish.PublishContent(PageRequest.GetInt("contentid", -1));
                        int preID = bllCmsContentModify.GetModel(PageRequest.GetInt("contentid", -1)).PrevID;
                        int nextID = bllCmsContentModify.GetModel(PageRequest.GetInt("contentid", -1)).NextID;
                        if (preID != 0)
                        {
                            publish.PublishContent(preID);
                        }
                        if (nextID != 0)
                        {
                            publish.PublishContent(nextID);
                        }
                    }
                    else if (PageRequest.GetInt("ContentState", -1) != 2)
                    {
                        //删除文件
                        KeRui.BLL.CmsNode bllCmsNode = new CmsNode();
                        KeRui.BLL.CmsContent bllCmsContent2 = new KeRui.BLL.CmsContent();
                        string dir = bllCmsNode.GetModel(bllCmsContent2.GetModel(PageRequest.GetInt("contentid", -1)).NodeID).NodeDir;
                        string file = dir + "\\" + PageRequest.GetInt("contentid", -1) + ".shtml";
                        if (File.Exists(file))
                        {
                            File.Delete(file);
                        }
                    }
                    //日志记录HYC
                    ManageHelper.AddLog("site_contentupdate_" + siteID, "修改了文章：“" + modelCmsContentModify.Title + "”。");
                    Response.Write("<script type=\"text/javascript\">window.close();</script>");
                  //  Response.End();
                    break;
                #endregion
            }

        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
            Response.Write("生成错误，请重新发布。");
            Response.End();
        }
    }

    #region 页面数据显示方法

    protected string BuildTemplateList(string input)
    {
        StringBuilder sb = new StringBuilder();
        KeRui.BLL.CmsSite bllCmsSite = new KeRui.BLL.CmsSite();
        KeRui.BLL.CmsTemplate bllCmsTemplate = new KeRui.BLL.CmsTemplate();
        KeRui.BLL.CmsTemplateFile bllCmsTemplateFile = new KeRui.BLL.CmsTemplateFile();
        int templateID = bllCmsTemplate.GetList("TemplateFolder='" +
                               bllCmsSite.GetModel(siteID).TemplateFolder + "'")[0].
            TemplateID;
        List<KeRui.Model.CmsTemplateFile> list = bllCmsTemplateFile.GetList(templateID, 3);
        KeRui.BLL.CmsContent bllCmsContent = new KeRui.BLL.CmsContent();
        int nodeID = PageRequest.GetInt("nodeid", -1) == -1
                         ? bllCmsContent.GetModelByCache(PageRequest.GetInt("contentid", -1)).NodeID
                         : PageRequest.GetInt("nodeid", -1);
        KeRui.BLL.CmsNode bllCmsNode = new KeRui.BLL.CmsNode();
        if (!File.Exists(bllCmsNode.GetModelByCache(nodeID).ContentDefaultTemplateFile))
        {
            sb.AppendFormat("<option value=\"{0}\">{0}</option>", "-没有模板文件-");
        }
        else
        {
            FileInfo fileInfo = new FileInfo(bllCmsNode.GetModelByCache(nodeID).ContentDefaultTemplateFile);
            sb.AppendFormat("<option value=\"{0}\">{0}</option>", fileInfo.Name);
            foreach (KeRui.Model.CmsTemplateFile model in list)
            {
                if (model.FileName.ToLower() != fileInfo.Name.ToLower())
                {
                    if (model.FileName == input)
                    {
                        sb.AppendFormat("<option value='{0}' checked='checked'>{0}</option>", model.FileName);
                    }
                    else
                    {
                        sb.AppendFormat("<option value='{0}'>{0}</option>", model.FileName);
                    }
                }
            }
        }
        return sb.ToString();
    }

    #region 栏目下拉列表
    protected string SelectNodeList(int pId, int intTemp)
    {
        string RetStr = "";
        KeRui.BLL.CmsNode bllCmsNode = new KeRui.BLL.CmsNode();

        List<KeRui.Model.CmsNode> list = bllCmsNode.GetListByPId(pId, bllCmsNode.GetModel(intTemp).SiteID);
        int j = 0;
        foreach (KeRui.Model.CmsNode model in list)
        {
            j++;
            string Empty = "";
            for (int i = 0; i < model.Depth; i++)
            {
                Empty += "　";
            }
            if (model.NodeID == intTemp)
            {
                RetStr += "<option value=" + model.NodeID + " selected>" + Empty + "{0}" + model.NodeName + "</option>";
            }
            else
            {
                RetStr += "<option value=" + model.NodeID + ">" + Empty + "{0}" + model.NodeName + "</option>";
            }
            if (model.ChildNum > 0)
            {
                RetStr += SelectNodeList(model.NodeID, intTemp);
            }
            RetStr = j == list.Count ? RetStr.Replace("{0}", "└") : RetStr.Replace("{0}", "├");
        }
        return RetStr;


    }
    #endregion

    #endregion

    #region 私有方法

    /// <summary>
    /// 动态生成字段表单
    /// </summary>
    /// <param name="modelIDForm">模块ID</param>
    /// <returns>字段表单</returns>
    private string GenerateForm(int modelIDForm)
    {
        KeRui.BLL.CmsField bllCmsField = new KeRui.BLL.CmsField();
        List<KeRui.Model.CmsField> list = bllCmsField.GetList("ModelID=" + modelIDForm + " AND IsUsing='True' AND FieldName NOT IN('Title','ContentID')");
        StringBuilder sb = new StringBuilder();
        foreach (CmsField model in list)
        {
            switch (model.FieldType)
            {
                case 1:  //单行
                    switch (model.FieldName.ToLower())
                    {
                        case "editor":
                            KeRui.BLL.Admin bllAdmin = new KeRui.BLL.Admin();
                            string Editor = bllAdmin.GetModel(KeRui.BLL.Account.GetLoginUserID()).RealName;
                            if (contentid != -1)
                            {
                                KeRui.BLL.CmsContentForModelid BLLCmsContentForModelid = new KeRui.BLL.CmsContentForModelid();
                                Editor = BLLCmsContentForModelid.GetModel("CmsContent0", contentid).Rows[0]["Editor"].ToString();
                            }

                            //hyc
                            sb.AppendFormat(
                                "<tr id='{0}'><td align='left' width='80px'><img name='help1' src='../pic/icons/help.gif' alt='{1}' onclick='helpInf(this)' />&nbsp;<span>{1}：</span></td><td colspan='3'><input name='txt{0}' type='text' readonly='readonly' value='{2}' class='colorblur' onfocus=\"this.className='colorfocus';\" onblur=\"this.className='colorblur';\" style='width:400px;' /></td></tr>", model.FieldName, model.FieldAlias, Editor);//bllAdmin.GetModel(KeRui.BLL.Account.GetLoginUserID()).RealName
                            break;
                        case "keyword":
                            IsShowGetKeywords = true;
                            sb.AppendFormat(
                                "<tr id='{0}'><td align='left' width='80px'><img name='help1' src='../pic/icons/help.gif' alt='{1}' onclick='helpInf(this)' />&nbsp;<span>{1}：</span></td><td colspan='3'><input name='txt{0}' type='text' id='txtkeywords' class='colorblur' onfocus=\"this.className='colorfocus';GetKeywords();\" onblur=\"this.className='colorblur';\" style='width:400px;' />&nbsp;<a href='javascript:void(0)' onclick='GetKeywords();return false;' >重新获取关键字</a>（以空格隔开）</td></tr>", model.FieldName, model.FieldAlias);
                            break;
                        default:
                            sb.AppendFormat(
                                "<tr id='{0}'><td align='left' width='80px'><img name='help1' src='../pic/icons/help.gif' alt='{1}' onclick='helpInf(this)' />&nbsp;<span>{1}：</span></td><td colspan='3'><input name='txt{0}' type='text'  class='colorblur' onfocus=\"this.className='colorfocus';\" onblur=\"this.className='colorblur';\" style='width:400px;' /></td></tr>", model.FieldName, model.FieldAlias);
                            break;
                    }
                    break;
                case 8:  //数字型
                    sb.AppendFormat(
                        "<tr id='{0}'><td align='left' width='80px'><img name='help1' src='../pic/icons/help.gif' alt='{1}' onclick='helpInf(this)' />&nbsp;<span>{1}：</span></td><td colspan='3'><input name='txt{0}' type='text'  class='colorblur' onfocus=\"this.className='colorfocus';\" onblur=\"this.className='colorblur';\" style='width:400px;' /></td></tr>", model.FieldName, model.FieldAlias);
                    break;
                case 2: //多行
                case 3: //多行，HTML
                    if (model.FieldName == "Content") //富文本编辑器
                    {
                        IsShowEditor = true;
                        sb.AppendFormat(
                        "<tr id='{0}'><td colspan='4'><textarea id='txtContent' name='txtContent'></textarea></td></tr>", model.FieldName);
                        sb.Append("<tr ><td colspan='4'> <input type=\"button\" class=\"button\" value=\"自动排版\" onclick=\"Automatic_layout();\" /><input type=\"button\" class=\"button\" value=\"清除空行\" onclick=\"Remove_blank_lines();\" /><input type=\"button\" class=\"button\" value=\"清除JS\" onclick=\"Clear_JS()\" /><input type=\"button\" class=\"button\" value=\"添加空行\" onclick=\"Add_a_blank_line()\" /><input type=\"button\" class=\"button\" value=\"统计数字\" onclick=\"Statistics_words()\" /><input type=\"button\" class=\"button\" value=\"清空内容\" onclick=\"Empty()\" /></td></tr>");
                    }
                    else
                    {
                        sb.AppendFormat(
                        "<tr id='{0}'><td align='left' width='80px'><img name='help1' src='../pic/icons/help.gif' alt='{1}' onclick='helpInf(this)' />&nbsp;<span>{1}：</span></td><td colspan='3'><textarea id='txt{0}' name='txt{0}' style='width:400px; height:120px;' class='colorblur' onfocus=\"this.className='colorfocus';\" onblur=\"this.className='colorblur';\"></textarea></td></tr>", model.FieldName, model.FieldAlias);

                    }
                    break;
                case 7: //下拉列表
                    sb.AppendFormat(
                       "<tr id='{0}'><td align='left' width='80px'><img name='help1' src='../pic/icons/help.gif' alt='{1}' onclick='helpInf(this)' />&nbsp;<span>{1}：</span></td><td colspan='3'><select name='txt{0}'>{2}</select></td></tr>", model.FieldName, model.FieldAlias, BuildSelectOption(model.StyleValue));
                    break;
                case 9: //日期型
                    sb.Append("<script type='text/javascript'>$(document).ready(function(){$('#txt" + model.FieldName + "').bind('focus', function(){WdatePicker({isShowClear:false,readOnly:false})});});</script>");
                    sb.AppendFormat(
                        "<tr id='{0}'><td align='left' width='80px'><img name='help1' src='../pic/icons/help.gif' alt='{1}' onclick='helpInf(this)' />&nbsp;<span>{1}：</span></td><td colspan='3'><input name='txt{1}' id='txt{1}' type='text' class='Wdate'/></td></tr>", model.FieldName, model.FieldAlias);
                    break;
                case 11: //字段附加属性
                    sb.AppendFormat(
                       "<tr id='{0}'><td align='left' width='80px'><img name='help1' src='../pic/icons/help.gif' alt='{1}' onclick='helpInf(this)' />&nbsp;<span>{1}：</span></td><td colspan='3'><select name='txt{0}'>{2}</select></td></tr>", model.FieldName, model.FieldAlias, BuildFieldAttOption(model.StyleValue, string.Empty));
                    break;
            }
        }
        return sb.ToString();
    }

    private static string BuildFieldAttOption(string className, string selected)
    {
        StringBuilder sb = new StringBuilder();
        KeRui.BLL.CmsFieldAttr bllCmsFieldAttr = new KeRui.BLL.CmsFieldAttr();
        List<KeRui.Model.CmsFieldAttr> list = bllCmsFieldAttr.GetList("ClassName='" + className + "'");
        foreach (KeRui.Model.CmsFieldAttr model in list)
        {
            if (model.AttributeName != selected)
            {
                sb.AppendFormat("<option value='{0}'>{0}</option>", model.AttributeName);
            }
            else
            {
                sb.AppendFormat("<option value='{0}' selected='selected'>{0}</option>", model.AttributeName);
            }
        }
        return sb.ToString();
    }

    /// <summary>
    /// 构建Option
    /// </summary>
    /// <param name="source">存储的字符串</param>
    /// <returns>构建的Option</returns>
    private static string BuildSelectOption(string source)
    {
        StringBuilder sb = new StringBuilder();
        string[] splitWord = { "\r\n" };
        string[] result = source.Split(splitWord, StringSplitOptions.RemoveEmptyEntries);
        foreach (string option in result)
        {
            sb.AppendFormat("<option value='{0}'>{0}</option>", option);
        }
        return sb.ToString();
    }

    #endregion
}
